//获取应用实例
const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
console.log(app)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    youthlistShow:true,
    youthlist:[],//青年之家列表
    searchval:'',//搜索
    type:1,//状态，1距离最近，2关注最多，3评价最高，4线上青年之家
    pagesize:10,
    page:1
  },
  //搜索
  onSearch(e){
    this.setData({
      searchval:e.detail,page:1,pagesize:10,youthlist:[]
    })
    this.getYouthHome(this.data.type,this.data.searchval)
  },
  onCancel(e){
    this.setData({searchval:e.detail,page:1,pagesize:10,youthlist:[]})
    this.getYouthHome(this.data.type,this.data.searchval)
  },
  //tab切换
  onChange(e){
    let ind = e.detail.index;
    //状态，1距离最近，2关注最多，3评价最高，4线上青年之家
    let type = 1
    if(ind == 0){
      type =  1   
    }else if(ind == 1){
      type = 2
    }else if(ind == 2){
      type = 3
    }else if(ind == 3){
      type = 4
    }
    this.setData({
      type:type,page:1,youthlist:[]
    })
    this.getYouthHome(this.data.type,this.data.searchval)
  },
  //青年之家
  async getYouthHome(type,keywords){
    let data={}
    data['flag'] = 'social_list'
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['status'] = type
    data['searchval'] = keywords
    data['cityName'] =app.globalData.address.city
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.forEach((item,index)=>{
        let imgArr = item.wj_url.split(",");
        item.wj_url = api.checkurl(imgArr[0])
        item.activity && item.activity.forEach((item1)=>{
          let picurl = item1.picurl.split(',')
          item1.picurl = api.checkurl(picurl)[0]
        })
        if( !item.id ){
          result.splice(index,1)
        }
      })
      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'youthlist['+(this.data.page-1)+']'
      
        this.setData({
          [str]:result,page,youthlistShow: str.length ? true:false,
        })
      }
    }else {
      Toast(message)
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({page:1,youthlist:[]})
    this.getYouthHome(this.data.type,this.data.searchval)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //this.getYouthHome(this.data.type,this.data.searchval)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getYouthHome(this.data.type,this.data.searchval)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})